<template>
  <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="100px" class="demo-ruleForm dict-form">
    <el-row :gutter="20">
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-form-item label="角色名称" prop="roleName">
            <el-input v-model="formData.roleName" disabled="disabled" />
          </el-form-item>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-form-item label="角色代码" prop="roleCode">
            <el-input v-model="formData.roleCode" disabled="disabled" />
          </el-form-item>
        </div>
      </el-col>
    </el-row>

    <el-form-item label="备注" prop="remark">
      <el-input v-model="formData.remark" type="textarea" disabled="disabled" />
    </el-form-item>

    <template v-for="(value,key) in permissions">
      <el-form-item :key="key" :label="key">
        <el-checkbox-group v-model="formData.permissionCodes">
          <template v-for="item in value">
            <el-checkbox :key="item.permissionCode" :label="item.permissionCode" name="permissionCodes" disabled="disabled">{{ item.permissionName }}</el-checkbox>
          </template>
        </el-checkbox-group>
      </el-form-item>
    </template>

    <el-form-item style="text-align: center;">
      <el-button @click="$router.back(-1)">返回</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import {getDataById, getPermission, getPermissionByRoleCode, save} from '@/api/sys/auth/role/index'

  export default {
  data() {
    return {
      owrPermission: [],
      permissions: [],
      formData: {
        roldeName: '',
        roleCode: '',
        permissionCodes: [],
        remark: '',
        id: this.$route.query.id
      },
      rules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 3, max: 100, message: '长度在 3 到 100 个字符', trigger: 'blur' }
        ],
        roleCode: [
          { required: true, message: '请输入角色代码', trigger: 'blur' },
          { min: 3, max: 100, message: '长度在 3 到 100 个字符', trigger: 'blur' }
        ],
        remark: [
          { required: false, message: '请填写备注', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    if (!this.$route.query.id) {
      this.$router.push({ name: 'role_mgr_list' })
    } else {
      this.getData()
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          save(this.formData).then(() => {
            this.loading = false
            this.$router.push({ name: 'role_mgr' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    getData() {
      getDataById(this.$route.query.id).then(res => {
        this.formData = res.data
        getPermissionByRoleCode(res.data.roleCode).then(res => {
          this.formData.permissionCodes = res.data
          getPermission().then(res => {
            this.permissions = res.data
          })
        })
      })
    }
  }
}
</script>

<style scoped>
  .dict-form{
    padding-top: 30px;
    padding-right: 30px;
  }
</style>
